package ru.housewife.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * abstract controller
 *
 * @author Alexander Troshanin
 */
public abstract class AbstractController {

    public boolean checkAuth(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return true;
    }

    protected static int getPageCount(int pageSize, int collectionSize) {
        return (collectionSize / pageSize) + (collectionSize % pageSize > 0 ? 1 : 0);
    }

    protected static List<Integer> generatePageNumsInfo(int page, int pageCount) {
        if (pageCount < 2) {
            return Collections.emptyList();
        }
        LinkedList<Integer> lst = new LinkedList<Integer>();
        for (int i = -2; i<=2; i++) {
            int pageNum = page + i;
            if (pageNum >= 0 && pageNum < pageCount) {
                lst.add(pageNum);
            }
        }
        int firstPageNum = lst.getFirst();
        if (firstPageNum > 1) {
            lst.addFirst(-1);
            lst.addFirst(0);
        } else if (firstPageNum > 0) {
            lst.addFirst(0);
        }
        int lastPageNum = lst.getLast();
        if (lastPageNum < pageCount - 2) {
            lst.add(-1);
            lst.add(pageCount - 1);
        } else if (lastPageNum < pageCount - 1) {
            lst.add(pageCount - 1);
        }
        return lst;
    }


}
